﻿Public Class frmLoadData

    Private m_currentUser As Withings.User

    Public Property CurrentUser() As Withings.User
        Get
            Return m_currentUser
        End Get
        Set(ByVal value As Withings.User)
            m_currentUser = value
        End Set
    End Property

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If m_currentUser IsNot Nothing Then
            Me.prtUser.SelectedObject = m_currentUser
            Me.txtPublicKey.Text = m_currentUser.PublicKey
            Me.txtUserID.Text = m_currentUser.UserId.ToString
        End If

    End Sub

    Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
        Me.lblPleaseWait.Visible = True
        Application.DoEvents()

        Me.TreeView1.Nodes.Clear()
        Dim getUserResponse As Withings.GetUserByIDResponse

        getUserResponse = Withings.User.GetUserByUserID(CInt(Me.txtUserID.Text), Me.txtPublicKey.Text)

        If getUserResponse.Status = Core.StatusResponseEnum.Successfull Then
            m_currentUser = getUserResponse.User
            Me.prtUser.SelectedObject = m_currentUser

            'jetzt die daten laden

            Dim measureResponse As Withings.GetMeasuresResponse
            measureResponse = m_currentUser.ReadMeasures(Nothing, Nothing, Nothing, , , , 20, )

            WriteMeasuresInTree(measureResponse)
            Me.lblErrorMessage.Visible = False
        Else
            Me.lblErrorMessage.Visible = True
            Me.lblErrorMessage.Text = getUserResponse.Status.ToString
        End If

        Me.lblPleaseWait.Visible = False
    End Sub

    Private Sub WriteMeasuresInTree(ByVal response As GetMeasuresResponse)
        Me.TreeView1.BeginUpdate()
        Me.TreeView1.Nodes.Clear()


        Me.TreeView1.Nodes.Add(New TreeNode(String.Format("{0}: {1}", "UpdateTime", response.UpdateTime.ToString)))
        Me.TreeView1.Nodes.Add(New TreeNode(String.Format("{0}: {1}", "HasMore", response.HasMore.ToString)))


        For Each grp As MeasureGroup In response.ReadMeasureGroups
            Dim groupNode As New TreeNode
            groupNode.Text = String.Format("{0}: {1}", "MeasureGroupID", grp.GroupId)

            groupNode.Nodes.Add(New TreeNode(String.Format("{0}: {1}", "AttributionStatus", grp.AttributionStatus.ToString)))
            groupNode.Nodes.Add(New TreeNode(String.Format("{0}: {1}", "Category", grp.Category.ToString)))
            groupNode.Nodes.Add(New TreeNode(String.Format("{0}: {1}", "MeasureGroupDate", grp.MeasureGroupDate.ToString)))

            Dim subNode As New TreeNode("Measures")
            For Each meas As Measure In grp.GetMeasures
                Dim measNode As New TreeNode
                measNode.Text = String.Format("{0} - {1}", _
                                              meas.MeasureType.ToString, meas.CalculateValue)
                subNode.Nodes.Add(measNode)
            Next
            groupNode.Nodes.Add(subNode)
            Me.TreeView1.Nodes.Add(groupNode)
        Next


        Me.TreeView1.EndUpdate()
    End Sub

End Class
